﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Snowdreamist.Text.UBB
{
    /// <summary>
    /// Align Type
    /// </summary>
    public enum AlignType
    {
        Right,
        Left,
        Center
    }

    internal class AlignFactory : PrefixUBBElementFactory
    {
        public AlignFactory() : base(ElementPrefix) { }

        public override UBBElement Create(UBBContent ubbContent)
        {
            AlignType type;

            var matchResult = ElementRegex.Match(ubbContent.Text);
            if (!matchResult.Success)
                return null;

            if (!Enum.TryParse<AlignType>(matchResult.Groups[1].Value, true, out type))
            {
                return null;
            }
            else
            {
                return new AlignElement(type);
            }
        }

        #region private

        public readonly static string ElementPrefix = "align";

        public readonly static Regex ElementRegex = new Regex("align=(center|left|right)", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

        #endregion private
    }

    /// <summary>
    /// Align element
    /// </summary>
    public class AlignElement : UBBElement
    {
        public const string Label = "align";

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="alignType"></param>
        public AlignElement(AlignType alignType)
            : base(Label)
        {
            this.AlignType = alignType;
        }

        /// <summary>
        /// Align type
        /// </summary>
        public AlignType AlignType { get; private set; }

        public override string SerializeToUBBCode()
        {
            return Label + "=" + this.AlignType.ToString();
        }

        public override object Clone()
        {
            return new AlignElement(this.AlignType);
        }
    }
}